<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
if (!defined("SelectGambleType_lib_include")){
	define("SelectGambleType_lib_include", 1);
	include_once(__Modules_Path."/Gamble.lib.php");

/**
 * 產生分盤選擇下拉清單
 * @param	strSelectName Select 下拉清單 Name
 * @param	intLType Lotto分類序號
 * @param	intBetType 下注種類
 * @param	intApointNum 指定號碼
 * @param	strIndex 已選擇的 Select Value 值
 * @return	字串 -> 成功，false -> 失敗
*/
	function MakeGambleTypeSelect($strSelectName, $intLType, $intBetType, $intApointNum, $strIndex){
		global $_Conn, $$_Conn;
		$strSQL =  "select ALine, BLine, CLine from `bet_SelectGambleType` where LType = ".$intLType." and BetType = ".$intBetType." and ApointNum = ".$intApointNum." order by ID";//, DLine

		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Select GambleType Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}
		
		$strCon = "<select name=\"".$strSelectName."[".$intBetType."][".$intApointNum."]"."\" style=\"color: #CC0000\">\n";
		while($Rows = PMA_mysql_fetch_object($RS)){
			$tmpIndex = sprintf("%.3f", $Rows -> ALine).",".sprintf("%.3f", $Rows -> BLine).",".sprintf("%.3f", $Rows -> CLine);//",".sprintf("%.3f", $Rows -> DLine);
			$strOption = "A: ".($Rows -> ALine*100)."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;B:".($Rows -> BLine*100)."&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;C:".($Rows -> CLine*100);//"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;D:".($Rows -> DLine*100);
			$strCon .= "<option value=\"".$tmpIndex."\"".(($strIndex == $tmpIndex)?" selected":"").">".$strOption."</option>\n";
		}
		$strCon .= "</select>\n";
		return $strCon;
	}  //  MakeGambleTypeSelect()  //


/**
 * 將分盤值，轉入 bet_LottoOdds Table
 * @param	intLID：Lotto序號，-1 -> 代表預設的限額值
 * @param	arrOdds：分盤種類組合值 "A Line Value, B Line Value, C Line Value"，arrOdds[x]，x -> 下注種類序號
 * @return	true -> 成功，false -> 失敗
*/
	function UpdateLottoOdds($arrOdds, $intLID){
		global $_ConnMaster;
		if(!is_array($arrOdds)){
			$GLOBALS["errormessage"] = "Odds Data not Array!!";
			$GLOBALS["errorhidden"] = "Line:".__LINE__."\nPage:".__FILE__;
			return false;
		}elseif(!is_int($intLID*1)){
			$GLOBALS["errormessage"] = "Lotto Serie No Data not Int!!";
			$GLOBALS["errorhidden"] = "Line:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$arrData = array();
		$strLog = "LottoID: ".$intLID."\n";
		foreach($arrOdds as $Ok => $arrOv){
			$intBT = (int)$Ok;

			foreach($arrOv as $ANk => $arrANv){
				$intApointNum = (int)$ANk;

				$arrLine = split(",", $arrANv);
				if(!is_array($arrLine)){
					$GLOBALS["errormessage"] = "Line Data Not Array!!";
					$GLOBALS["errorhidden"] = "GambleType:".$vv."\nLine:".__LINE__."\nPage:".__FILE__."\nFile: ".$_SERVER["PHP_SELF"];
					return false;
				}

				foreach($arrLine as $Lk => $Lv){
					$Lk += 1;
					$flGap = ($Lv - $arrLine[0]) / 2;

					$arrData[] = "('".$intLID."','".$Lk."','".$intBT."','".$intApointNum."','".$Lv."','".($Lv - $flGap)."','".$flGap."')";
					$strLog .= "BT: ".$intBT."  == Line: ".$Lk."  == ApointNum: ".$intApointNum."  == SunValue: ".$Lv."  == MaxValue: ".($Lv - $flGap)."  == MinValue: ".$flGap."\n";
				} // foreach($arrLine as $Lk => $Lv) //
			}// foreach($arrOv as $ANk => $arrANv) //
		} // foreach($arrOdds as $Ok => $arrOv) //

		if(sizeof($arrData) > 0){
			$strSQL = "INSERT INTO `bet_LottoOdds`(`LottoID`, `GambleType`, `Bet_Type`, `ApointNum`, `SunValue`, `MaxValue`, `MinValue`) VALUES".implode(",", $arrData)." ON DUPLICATE KEY UPDATE `SunValue` = VALUES(`SunValue`), `MaxValue` = VALUES(`MaxValue`), `MinValue` = VALUES(`MinValue`)";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				$GLOBALS["errormessage"] = "Update Lotto Odds Data Error!!";
				$GLOBALS["errorhidden"] = "SQL: ".$strSQL."\nError: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;

				$strLog .= $GLOBALS["errorhidden"];
				AddData_Log(0, "變更樂透分盤值資料失敗", $strLog);
				return false;
			}
		}
		AddData_Log(0, "變更樂透分盤值資料成功", $strLog);

		return true;
	}  // UpdateLottoOdds()  //

/**
 * 取得樂透分盤資料
 * @param	intLID：樂透序號
 * @return	Array[x][y] -> 成功，false -> 失敗
 * x -> 下注種類序號，y -> 分盤種類序號
*/
	function GetArrayLottoGambleTypeData($intLID){
		global $_Conn, $$_Conn;
		$strSQL = "select * from `bet_LottoOdds` where `GambleType` < 10 and `LottoID` = ".$intLID;
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Lotto Odds Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$arrOdd = array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrOdds[$Rows -> Bet_Type][$Rows -> ApointNum][$Rows -> GambleType] = sprintf("%.3f", $Rows -> SunValue);
		}

		return $arrOdds;
	}  // GetArrayLottoGambleTypeData //


/**
 * 取得樂透賠率兌換資料
 * @param	intLID：樂透序號
 * @return	Array[x][y] -> 成功，false -> 失敗
 * x -> 下注種類序號，y -> 分盤種類序號
*/
	function getArrayLottoChRateData($intLID){
		global $_Conn, $$_Conn;
		$strSQL = "select * from `bet_LottoOdds` where (`GambleType` between '11' and '20') and `LottoID` = ".$intLID;
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Lotto Odds Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		$arrOdd = array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrOdds[$Rows -> Bet_Type][$Rows -> ApointNum][$Rows -> GambleType] = sprintf("%.3f", $Rows -> SunValue);
		}

		return $arrOdds;
	}  // getArrayLottoChRateData //


/**
 * 變更樂透賠率兌換資料
 * @param	intLID 樂透序號
 * @param	arrChRate 賠率兌換資料陣列
 * @return	true -> 成功，false -> 失敗
*/
	function setLottoChRateData($intLID, $arrChRate){
		global $_Conn, $$_Conn;

		if(!is_array($arrChRate)){
			$GLOBALS["errormessage"] = "Not Array Data!!";
			return false;
		}
		$arrData = array();
		$strLog = "LottoID: ".$intLID."\n";
		$strParam = '<?php'."\n";
		foreach($arrChRate as $intBT => $arrTmpRate){
			if(!is_array($arrTmpRate)){
				$GLOBALS["errormessage"] = "Not Array Data!!";
				return false;
			}
			$intBT = (int)$intBT;

			foreach($arrTmpRate as $intAPN => $arrRate){
				if(!is_array($arrTmpRate)){
					$GLOBALS["errormessage"] = "Not Array Data2!!";
					return false;
				}
				$intAPN = (int)$intAPN;

				foreach($arrRate as $intLine => $flRate){
					$intLine = (int)$intLine;
					$flRate = (float)$flRate;
					if($intLine < 11 && $intLine > 20)continue;

					$arrData[] = "('".$intLID."','".$intLine."','".$intBT."','".$intAPN."','".$flRate."')";
					$strLog .= "BT: ".$intBT."  == Line: ".$intLine."  == Rate: ".$flRate."\n";
					$strParam .= '$_arrChRate['.$intBT.']['.$intAPN.']['.$intLine.'] = '.$flRate.";\n";
				}
			}
		}
		$strParam .= '?>';

		if(sizeof($arrData) > 0){
			$strSQL = "INSERT INTO `bet_LottoOdds`(`LottoID`, `GambleType`, `Bet_Type`, `ApointNum`, `SunValue`) VALUES".implode(",", $arrData)." ON DUPLICATE KEY UPDATE SunValue = VALUES(`SunValue`)";
			if(!PMA_mysql_query($strSQL, $_ConnMaster)){
				$GLOBALS["errormessage"] = "Update Lotto Odds Data Error!!";
				$GLOBALS["errorhidden"] = "SQL: ".$strSQL."\nError: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;

				$strLog .= $GLOBALS["errorhidden"];
				AddData_Log(0, "變更樂透賠率兌換資料失敗", $strLog);
				return false;
			}
		}
		$strPath = __Common_Path."/CommToOddsRate_".$intLID.".inc.php";
		if(!writeFile($strPath, $strParam, "w")){
			$strLog .= $GLOBALS["errorhidden"];
			AddData_Log(0, "變更樂透賠率兌換資料寫檔失敗", $strLog);
			return false;
		}
		AddData_Log(0, "變更樂透賠率兌換資料成功", $strLog);

		return true;
	}  // setLottoChRateData //

/**
 * 當分盤賠率改變後，現有賠率也跟的自動調整
 * @param	intLID：樂透序號
 * @return	true -> 成功，false -> 失敗
*/
	function ChangeGambleTypeOdds($intLID){
		global $_ConnMaster;
		include(__Common_Path."/gambletype/GambleType1_".$intLID.".inc.php");

		$arrTwoOdds = array();
		$arrTwoOdds[1] = "2,3,5,6,12,13,16,27";
		$arrTwoOdds[2] = "52,53,55,56,58,59,62,63,84,85,86";
		$arrTwoOdds[3] = $arrTwoOdds[2];
		$arrTwoOdds[4] = "14,15,16,17";
		$arrTwoOdds[5] = "2,3,4,9,10,11,12,13";
		$arrTwoOdds[6] = $arrTwoOdds[2];
		$arrTwoOdds[7] = $arrTwoOdds[2];
		$arrTwoOdds[8] = $arrTwoOdds[2];

		$strSQL = "select `GambleID`, `Bet_Type`, `Number`, `Odds` from `bet_TodayGame".$intLID."` where `LottoID` = ".$intLID." and `Bet_Type` in (".$arrTwoOdds[$intLID].")";
		if(!$RS = @PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Find Today Game Odds Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($_ConnMaster)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}
		$arrOdds = array();
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrOdds[$Rows -> Bet_Type][$Rows -> Number] = $Rows -> Odds;
			$intGID = $Rows -> GambleID;
		}
		
		PMA_mysql_query("BEGIN", $_ConnMaster);
		$intApointNum = 0;
		foreach($arrOdds as $intBT => $arrNOdds){
			$tolOdds = $arrNOdds[0] + $arrNOdds[1];
			if(intval($tolOdds*10000) == intval($_arrGambleTypeAllOdds[$intBT][$intApointNum]*10000))continue;
			$flGap = ($tolOdds - $_arrGambleTypeAllOdds[$intBT][$intApointNum]) / 2;

			//輸入單分或大賠率
			if(!$intOID = InsertBetTypeOdds($intGID, $intLID, $intBT, 0, $arrNOdds[0] - $flGap, 0, $_SESSION["Admin_Info"] -> Account)){
				@PMA_mysql_query("ROLLBACK", $_ConnMaster);
				return false;
			}
			if(!UpdateTodayGame($intGID, $intLID, $intBT, 0, $intOID, $arrNOdds[0] - $flGap, ", tolJumpStake = 0")){
				@PMA_mysql_query("ROLLBACK", $_ConnMaster);
				return false;
			}

			//輸入雙分或小盤賠率
			if(!$intOID2 = InsertBetTypeOdds($intGID, $intLID, $intBT, 1, $arrNOdds[1] - $flGap, 0, $_SESSION["Admin_Info"] -> Account)){
				@PMA_mysql_query("ROLLBACK", $_ConnMaster);
				return false;
			}
			if(!UpdateTodayGame($intGID, $intLID, $intBT, 1, $intOID2, $arrNOdds[1] - $flGap, ", tolJumpStake = 0")){
				@PMA_mysql_query("ROLLBACK", $_ConnMaster);
				return false;
			}
		}  //foreach($arrOdds as $intBT => $arrNOdds){
		@PMA_mysql_query("COMMIT", $_ConnMaster);
		return true;
	}
//-------------------------------------------------------//
//SelectGambleType_lib End
}
?>